source('00_util_scripts/mod_bplot.R')

ena.meta <- read_delim('~/alaria/joyner22llpc/nfcore_input.csv')

ena.meta

sra.meta <- read_delim('mission/Fc_glyco/joyner22asc.sra.meta.csv')

sra.meta |> DT::datatable()

id.meta <- sra.meta |>
  mutate(readgroup = subject_id,
         tissue, run = Run, .keep = 'none')

id.meta <- ena.meta |>
  mutate(run = str_extract(fastq_1, 'SRR\\d+')) |>
  left_join(id.meta)

bam.path <- list.files('~/append-ssd/joyner22asc/star_salmon/', 'bam$',
           full.names = T)

bam.meta <- tibble(bam = bam.path,
                   sample = str_extract(bam, 'RNA.+(?=\\.sort)')) |>
  inner_join(id.meta)

bm.decml <- read_csv('bm.decimal.csv')

bam.meta |>
  filter(tissue != 'Blood') |>
  distinct(readgroup)

bam.meta |>
  mutate(readgroup = str_c('R', readgroup), bam, .keep = 'none') |>
  write_csv('joyner22bm.rnavar.csv')

# chr XY by samtools idxstats --------
chr.xy <- read_delim('samtools-idxstats-xy-plot.tsv')

chr.xy |>
  left_join(bam.meta, join_by(Sample == sample)) |>
  mutate(individual = as.character(readgroup) |> fct_reorder(`Chromosome Y`)) |>
  ggplot(aes(individual, `Chromosome Y`)) +
  geom_boxplot()
